package com.cipe.cmrs.controllers;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.Paymentrequest;
import com.cipe.cmrs.service.AccountCodeService;
import com.cipe.cmrs.service.CipeprojectService;
import com.cipe.cmrs.service.CountryService;
import com.cipe.cmrs.service.PayeeService;
import com.cipe.cmrs.service.PaymentrequestService;
import com.cipe.cmrs.service.PaymenttypeService;
import com.cipe.cmrs.service.UserService;
import com.cipe.cmrs.validator.PaymentRequestValidator;

@Controller
public class PaymentrequestController extends BaseController {

	protected Log log = LogFactory.getLog(getClass());
	private static final long		serialVersionUID	= 1815579886981166259L;
	@Autowired
	private PaymentrequestService	paymentrequestService;
	@Autowired
	private CountryService			countryService;
	@Autowired
	private PaymenttypeService paymenttypeService;
	@Autowired
	private UserService				userService;
	// @Autowired
	// private PaymentmethodService paymentmethodService;
	@Autowired
	private PayeeService			payeeService;
	@Autowired
	private CipeprojectService cipeprojectService;
	@Autowired
	private AccountCodeService accountCodeService;
	@Autowired
	private PaymentRequestValidator validator;
	
	private String editPaymentrequestView = "editPaymentrequest";

	@RequestMapping("/secure/findPaymentrequest.do")
    public String findPaymentrequest(@ModelAttribute("paymentrequest") Paymentrequest paymentrequest,
        @RequestParam(value = "initiateSearch", required = false) String initiateSearch, Model model) {
		model.addAttribute("paymentrequest", new Paymentrequest());
		model.addAttribute("countryList", countryService.findAllOrderBy("displayName"));
		// model.addAttribute("paymentrequesttypeList",
		// paymentrequesttypeService.findAll());
		model.addAttribute("userList", userService.findAllOrderBy("firstName") );
		// model.addAttribute("paymentmethodList",
		// paymentmethodService.findAll());
		model.addAttribute("accountCodeList", accountCodeService.findAllOrderBy("accountCode") );
		model.addAttribute("projectList", cipeprojectService.findAllOrderBy("costCenterNumber") );
		model.addAttribute("paymenttypeList",paymenttypeService.findAllOrderBy("paymenttypename"));
        model.addAttribute("payeeList", payeeService.findAllOrderBy("payeeName"));
		//model.addAttribute("payeeList", payeeService.getAllActivePayees());
        System.out.println(" paymentrequest" + paymentrequest.getPayee());
        if (StringUtils.isNotBlank(initiateSearch)) {
            model.addAttribute("paymentrequestList", paymentrequestService.findPaymentrequest(paymentrequest));
            model.addAttribute("paymentrequest", paymentrequest);
            model.addAttribute("initiateSearch", initiateSearch);
        }
		return "findPaymentrequest";
	}
	@RequestMapping("/secure/findSearchPaymentrequest.do")
    public String findSearchPaymentrequest(@ModelAttribute("paymentrequest") Paymentrequest paymentrequest,
        @RequestParam(value = "initiateSearch", required = false) String initiateSearch, Model model) {
		model.addAttribute("paymentrequestList", paymentrequestService.findPaymentrequest(paymentrequest));
		return "searchFindPaymentRequest";	
	}	
	@RequestMapping("/secure/findResultPaymentrequestWithprocedure.do")
	public String findResultPaymentrequestWithprocedure(@ModelAttribute("paymentrequest") Paymentrequest paymentrequest, Model model) {
		model.addAttribute("paymentrequest", paymentrequest);
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("userList", userService.findAll());
		model.addAttribute("accountCodeList", accountCodeService.findAll() );
		model.addAttribute("projectList", cipeprojectService.findAll() );
		model.addAttribute("paymenttypeList",paymenttypeService.findAll());
		model.addAttribute("paymentrequestList", paymentrequestService.findPaymentrequest(paymentrequest.getPrc()));
		model.addAttribute("initiateSearch", "initiateSearch");
		model.addAttribute("message", paymentrequest.getMessage());
		return "findPaymentrequestDashboard";
	}
	
	
	
	@RequestMapping("/secure/showPaymentrequestList.do")
	public String showPaymentrequestList(Model model) {
		model.addAttribute("paymentrequests", paymentrequestService.findAll());
		return "paymentrequestList";
	}
}
